﻿using CPPEI.Coolzon.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.DataProvider.Task
{
    public class DataLoadTask
    {
        public string Key { get; private set; }
        private string _userIdentity;
        private Metadata.DataSource _dataSource;
        private Metadata.DataSet _dataSet;
        private List<QueryParameter> _parameters;
        private IDataProvider _dataProvider;

        public DataResult DataResult { get; private set; }

        private object _lockObj = new object();
        private object _lockRefObj = new object();

        private int _refCount;

        private EventWaitHandle _waitEventHandle;

        internal DataLoadTask(string taskKey,
            string userIdentity,
            CPPEI.Coolzon.Metadata.DataSource dataSource,
            Metadata.DataSet dataSet,
            List<QueryParameter> parameters,
            IDataProvider dataProvider)
        {
            this.Key = taskKey;
            this._userIdentity = userIdentity;
            this._dataSource = dataSource;
            this._dataSet = dataSet;
            this._parameters = parameters;
            this._dataProvider = dataProvider;
        }

        public void Run()
        {
            if (_waitEventHandle != null)
            {
                //Common.Logger.Debug("data task key:[{0}] is running, waiting...", Key);
                _waitEventHandle.WaitOne();
                //Common.Logger.Debug("data task key:[{0}] is run finish", Key);
            }
            else
            {
                lock (_lockObj)
                {
                    if (_waitEventHandle != null)
                    {
                       // Common.Logger.Debug("data task key:[{0}] is running, waiting...", Key);
                        _waitEventHandle.WaitOne();
                        //Common.Logger.Debug("data task key:[{0}] is run finish", Key);
                    }
                    else
                    {
                        //Common.Logger.Debug("data task key:[{0}] creating and running...", Key);
                        _waitEventHandle = new EventWaitHandle(false, EventResetMode.ManualReset);

                        try
                        {
                            DataResult = _dataProvider.QueryData(_dataSource, _dataSet, _parameters);
                        }
                        catch(Exception ex)
                        {
                            Common.Logger.ErrorToTag("DataLoadTask", "data task key:[{0}] exception:{1}", Key, ex);
                        }
                        //Common.Logger.Debug("data task key:[{0}] create and run finish", Key);
                        _waitEventHandle.Set();
                    }
                }
            }
        }

        internal void Ref()
        {
            lock (_lockRefObj)
            {
                _refCount++;
            }
        }

        internal void Release()
        {
            lock (_lockRefObj)
            {
                _refCount--;
            }
        }

        public bool IsCanDispose()
        {
            if (_refCount == 0)
            {
                return true;
            }
            return false;
        }

        public void Wait()
        {
            _waitEventHandle.WaitOne();
        }
    }
}
